package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;

import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cargo/factory")
public class FactoryController extends BaseController {

    @Reference
    private FactoryService factoryService;

    /*
    作用：工厂 分页列表
    url:/cargo/factory/list.do
    参数：
    返回值：cargo/factory/factory-list
     */

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){

        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCompanyIdEqualTo(getLoginUserCompanyId());
        PageInfo<Factory> pageInfo = factoryService.findByPage(factoryExample, pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "cargo/factory/factory-list";
    }

    /*
    作用：进入新建页面
    url:http://localhost:8080/cargo/factory/toAdd.do
    参数：
    返回值：cargo/factory/factory-add
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "cargo/factory/factory-add";
    }

    /*
   作用：进入新建页面
   url:http://localhost:8080/cargo/factory/edit.do
   参数：工厂对象
   返回值：cargo/factory/list.do
    */
    @RequestMapping("/edit")
    public String edit(Factory factory){

        //补全数据
        factory.setCompanyId(getLoginUserCompanyId());
        factory.setCreateBy(getLoginUser().getId());
        factory.setCreateDept(getLoginUser().getDeptId());

        if(StringUtils.isEmpty(factory.getId())){
            factoryService.save(factory);
        }else {
            factoryService.update(factory);
        }
        return "redirect:/cargo/factory/list.do";
    }

    /*
   作用：进入更新页面
   url:http://localhost:8080/cargo/factory/toUpdate.do
   参数：id
   返回值：cargo/factory/factory-update
    */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){

        Factory factory = factoryService.findById(id);
        request.setAttribute("factory",factory);
        return "cargo/factory/factory-update";
    }

    /*
  作用：删除
  url:http://localhost:8080/cargo/factory/delete.do
  参数：id
  返回值：
   */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String id){
        HashMap<String, Object> resultInfo = new HashMap<>();
        boolean success = factoryService.delete(id);
        if (success){
            resultInfo.put("message","删除成功");
            resultInfo.put("success",true);
        }
        return resultInfo;
    }

    /*
  url:${ctx}/cargo/factory/toView.do?id="+id;
  作用： 查看厂家信息
  参数： id
*/
    @RequestMapping("/toView")
    public String toView(String id){
        Factory factory = factoryService.findById(id);
        request.setAttribute("factory",factory);
        return "cargo/factory/factory-view";
    }
}
